package com.example.demo;

import com.google.common.collect.ImmutableMap;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * TODO
 *
 * @author funcas
 * @since 1.0
 */
@ControllerAdvice
public class Oauth2ExceptionHandler {
    @ResponseBody
    @ExceptionHandler(value = OAuth2Exception.class)
    public Object handleOauth2(OAuth2Exception e) {
        return ImmutableMap.of("code", e.getHttpErrorCode(), "msg", e.getMessage(), "data", "");
    }

    @ResponseBody
    @ExceptionHandler(AuthenticationException.class)
    public Object globalException(AuthenticationException e) {
        e.printStackTrace();
        return ImmutableMap.of("code", "000401", "msg", e.getMessage());
    }
}
